package in.muton.edushare.wicket.converters.choicerenderers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.vo.Courses;

public class CourseChoiceRenderer implements IChoiceRenderer<String> {
	private static final long serialVersionUID = -2047509326441602522L;

	public CourseChoiceRenderer(List<IModel<Courses>> courseList){
		int i = 0;
		courseMap = new HashMap<Integer, IModel<Courses>>();
		for(IModel<Courses> c:courseList){
			courseMap.put(i++, c);
		}
	}
	
	public List<String> getListOfCourses(){
		List<String> l = new ArrayList<String>();
		Set<Entry<Integer,IModel<Courses>>> set =  courseMap.entrySet();
		for(Entry<Integer,IModel<Courses>> e:set){
			if(firstCourse == null)
				firstCourse = e.getValue();
			l.add(e.getValue().getObject().getCourseName());			
		}
		if(l.size() == 0)
			l.add("");
		return l;
	}
	
	public Object getDisplayValue(String arg0) {
		return arg0;
	}

	public String getIdValue(String arg0, int arg1) {
		Set<Entry<Integer,IModel<Courses>>> set =  courseMap.entrySet();
		for(Entry<Integer,IModel<Courses>> e:set){
			if(arg0.equals(e.getValue().getObject().getCourseName()))
				return e.getValue().getObject().getCourseId().toString();
		}
		return "0";
	}
	
	public IModel<String> getModel(){
		if(firstCourse != null){
			return new Model<String>(firstCourse.getObject().getCourseName());
		}else
			return new Model<String>("");
	}

	private HashMap<Integer,IModel<Courses>> courseMap;
	private IModel<Courses> firstCourse = null;
}
